//
//  SHNoticeView.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/10/19.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//

#import "SHNoticeView.h"
@interface SHNoticeView()
@property(nonatomic,strong)UIButton *leftImg;
@property(nonatomic,strong)SHLabel *titleLabel;
@property(nonatomic,strong)SHButton *deleteBtn;


@end
@implementation SHNoticeView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithHexString:@"#fdeded"];
        [self addSubview:self.titleLabel];
        //[self addSubview:self.deleteBtn];
        [self addSubview:self.leftImg];
        [self.leftImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.centerY.equalTo(self);
            make.width.equalTo(@__I6Width(46));
            make.height.equalTo(@__I6Height(36));

        }];
        self.titleLabel.text = title;
        [self beginPaoMadengAnimations];
        [self addTapGestureRecognizer];
//        // app退到后台
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationWillResignActiveNotification object:nil];
        // app进入前台
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterPlaygroundNotice) name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    return self;
    
}

- (void)appDidEnterPlaygroundNotice{
    
    [self beginPaoMadengAnimations];

}
- (SHLabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [SHLabel labelWithTextFont:14 textColor:[UIColor colorWithHexString:@"#ff9948"] textAlignment:NSTextAlignmentLeft];

        _titleLabel.frame = CGRectMake(__I6Width(34), __I6Height(10),1000, __I6Height(15));
        
       
        
       
    }
    
    return _titleLabel;
}

- (UIButton *)leftImg{
    
    if (!_leftImg) {
        
        _leftImg = [[UIButton alloc]init];
        _leftImg.backgroundColor = [UIColor colorWithHexString:@"#fdeded"];
        [_leftImg setImage:[UIImage imageNamed:@"shcashtrumpet"] forState:UIControlStateNormal];
//        _leftImg.layer.shadowColor = [UIColor lightGrayColor].CGColor;
//        _leftImg.layer.shadowOffset = CGSizeMake(__I6Width(1),__I6Width(-3));
        _leftImg.layer.cornerRadius = __I6Width(1);
//        _leftImg.layer.shadowOpacity = .8;
    }
    
    
    return _leftImg;
}

- (SHButton *)deleteBtn{
    
    if (!_deleteBtn) {
        
        _deleteBtn = [SHButton buttonWithType:UIButtonTypeCustom title:@"" font:16 titleColor:[UIColor blueColor] textAlignment:0 forState:UIControlStateNormal];
        [_deleteBtn setImage:[UIImage imageNamed:@"关闭@2x"] forState:UIControlStateNormal];
        [_deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(__I6Width(10),__I6Width(10), __I6Width(10), __I6Width(10))];
        _deleteBtn.frame = CGRectMake(KScreenWidth - __I6Width(35), 0,__I6Width(35),__I6Width(35));
        [_deleteBtn addTarget:self action:@selector(removeNotice:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _deleteBtn;
}

#pragma mark - 开始跑马灯动画
- (void)beginPaoMadengAnimations{
    
    CGRect frame = _titleLabel.frame;
    frame.origin.x = KScreenWidth;
    _titleLabel.frame = frame;
    [UIView beginAnimations:@"paomadeng"context:NULL];
    [UIView setAnimationDuration:8.8f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationRepeatCount:999999];
    frame = _titleLabel.frame;
    CGFloat width  = [self.titleLabel.text boundingRectWithSize:CGSizeMake(KScreenWidth, 36) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :KSystemFont(13)
     } context:nil].size.width;
    frame.origin.x = - width ;
   
    _titleLabel.frame = frame;
    [UIView commitAnimations];
  
}

#pragma mark - 点击手势
- (void)addTapGestureRecognizer{
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapNoticeView:)];
    [self addGestureRecognizer:tapGes];
}
#pragma mark - 移除通知页面
- (void)removeNotice:(SHButton *)sender{
    
    if ([_delegate respondsToSelector:@selector(removeNoticeView:)]) {
        [_delegate removeNoticeView:self];

    }


}

#pragma mark - 查看通知详情
- (void)tapNoticeView:(UITapGestureRecognizer *)ges{
    
    if ([_delegate respondsToSelector:@selector(lookNoticeDetail:)]) {
        [_delegate lookNoticeDetail:self];
        
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
